package com.lekang.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.lekang.common.core.domain.entity.SysUser;
import com.lekang.common.utils.DateUtils;
import com.lekang.domain.NursingLevelYyh;
import com.lekang.le.domain.NursingItem;
import com.lekang.le.domain.NursingItemPlanBinding;
import com.lekang.le.domain.NusingPlan;
import com.lekang.le.mapper.NursingItemMapper;
import com.lekang.le.mapper.NursingItemPlanBindingMapper;
import com.lekang.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lekang.mapper.NursingPlan_yyhMapper;
import com.lekang.domain.NursingPlan_yyh;
import com.lekang.service.INursingPlan_yyhService;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-13
 */
@Service
public class NursingPlan_yyhServiceImpl implements INursingPlan_yyhService 
{
    @Autowired
    private NursingPlan_yyhMapper nursingPlan_yyhMapper;
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private NursingItemPlanBindingMapper nursingItemPlanBindingMapper;
    @Autowired
    private NursingItemMapper nursingItemMapper;

    /**
     * 查询护理计划
     * 
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlan_yyh selectNursingPlan_yyhById(Long id)
    {
        return nursingPlan_yyhMapper.selectNursingPlan_yyhById(id);
    }

    /**
     * 查询护理计划列表
     * 
     * @param nursingPlan_yyh 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan_yyh> selectNursingPlan_yyhList(NursingPlan_yyh nursingPlan_yyh)
    {
        List<NursingPlan_yyh> nursingPlanYyhs = nursingPlan_yyhMapper.selectNursingPlan_yyhList(nursingPlan_yyh);
        for(NursingPlan_yyh np : nursingPlanYyhs){
            Long creator = np.getCreator();
            SysUser sysUser = sysUserMapper.selectUserById(creator);
            np.setSysUser(sysUser);
//            System.out.println(np);
        }
        return nursingPlanYyhs;
    }

    @Override
    public int selectNursingPlanName(NursingPlan_yyh nursingPlan_yyh)
    {
        List<NursingPlan_yyh> nursingPlanYyhs = nursingPlan_yyhMapper.selectNursingPlan_yyhList2(nursingPlan_yyh);
        if(nursingPlanYyhs.size() > 0){
            return 1;
        }
        else {
            return 0;
        }


    }

    /**
     * 新增护理计划
     * 
     * @param nursingPlan_yyh 护理计划
     * @return 结果
     */
    @Override
    public int insertNursingPlan_yyh(NursingPlan_yyh nursingPlan_yyh)
    {
        nursingPlan_yyh.setCreateTime(new Date());
        //计算价格
        BigDecimal price = BigDecimal.ZERO;
        for(NursingItemPlanBinding nn : nursingPlan_yyh.getNursingItems()){
            NursingItem ni = nursingItemMapper.selectNursingItemById(nn.getNursingItemId());
            if(nn.getExecutionCycle()==1){
                price = price.add(ni.getPrice().multiply(new BigDecimal(30)));
            } else if (nn.getExecutionCycle()==2) {
                price = price.add(ni.getPrice().multiply(new BigDecimal(4)));
            }else {
                price = price.add(ni.getPrice());
            }
        }
        nursingPlan_yyh.setPrice(price);
        int i = nursingPlan_yyhMapper.insertNursingPlan_yyh(nursingPlan_yyh);
        NursingPlan_yyh nursingPlanYyh = new NursingPlan_yyh();
        nursingPlanYyh.setPlanName(nursingPlan_yyh.getPlanName());
        List<NursingPlan_yyh> nursingPlanYyhs = nursingPlan_yyhMapper.selectNursingPlan_yyhList(nursingPlanYyh);
        Long id = nursingPlanYyhs.get(0).getId();
        //向关联表中插数据
//        System.out.println("向关联表中插数据");
//       System.out.println(nursingPlan_yyh);
        NursingItemPlanBinding[] nursingItems = nursingPlan_yyh.getNursingItems();
        for(NursingItemPlanBinding nn : nursingItems){
            NursingItemPlanBinding nursingItemPlanBinding = new NursingItemPlanBinding();
            nursingItemPlanBinding.setNursingPlanId(id);
            nursingItemPlanBinding.setNursingItemId(nn.getNursingItemId());
            nursingItemPlanBinding.setServiceTime(nn.getServiceTime());
            nursingItemPlanBinding.setExecutionCycle(nn.getExecutionCycle());
            nursingItemPlanBinding.setExecutionFrequency(nn.getExecutionFrequency());
            nursingItemPlanBindingMapper.insertNursingItemPlanBinding(nursingItemPlanBinding);
            NursingItem nursingItem = nursingItemMapper.selectNursingItemById(nn.getNursingItemId());
            price = price.add(nursingItem.getPrice());
        }

        nursingPlan_yyh.setCreateTime(DateUtils.getNowDate());
        return i;
    }

    /**
     * 修改护理计划
     * 
     * @param nursingPlan_yyh 护理计划
     * @return 结果
     */
    @Override
    public int updateNursingPlan_yyh(NursingPlan_yyh nursingPlan_yyh)
    {
//        System.out.println("修改plan时获取的对象：");
//        System.out.println(nursingPlan_yyh);
        NursingPlan_yyh nursingPlanYyh = new NursingPlan_yyh();
        nursingPlanYyh.setPlanName(nursingPlan_yyh.getPlanName());
        List<NursingPlan_yyh> nursingPlanYyhs = nursingPlan_yyhMapper.selectNursingPlan_yyhList(nursingPlanYyh);
        Long id = nursingPlanYyhs.get(0).getId();
        NursingItemPlanBinding[] nursingItems = nursingPlan_yyh.getNursingItems();
        if(nursingItems != null){
            //修改中间表数据前清空之前关联的护理项目

        nursingItemPlanBindingMapper.deleteNursingItemPlanBindingByPlanId(id);
        for(NursingItemPlanBinding np : nursingItems){
            System.out.println(np);
            nursingItemPlanBindingMapper.updateNursingItemPlanBinding(np);
        }}
        //向中间表中插入数据
        for(NursingItemPlanBinding nn : nursingItems){
            NursingItemPlanBinding nursingItemPlanBinding = new NursingItemPlanBinding();
            nursingItemPlanBinding.setNursingPlanId(id);
            nursingItemPlanBinding.setNursingItemId(nn.getNursingItemId());
            nursingItemPlanBinding.setServiceTime(nn.getServiceTime());
            nursingItemPlanBinding.setExecutionCycle(nn.getExecutionCycle());
            nursingItemPlanBinding.setExecutionFrequency(nn.getExecutionFrequency());
            nursingItemPlanBindingMapper.insertNursingItemPlanBinding(nursingItemPlanBinding);
        }
        //更新价格
        BigDecimal price = BigDecimal.ZERO;
        for(NursingItemPlanBinding nn : nursingPlan_yyh.getNursingItems()){
            NursingItem ni = nursingItemMapper.selectNursingItemById(nn.getNursingItemId());
            if(nn.getExecutionCycle()==1){
                price = price.add(ni.getPrice().multiply(new BigDecimal(30)));
            } else if (nn.getExecutionCycle()==2) {
                price = price.add(ni.getPrice().multiply(new BigDecimal(4)));
            }else {
                price = price.add(ni.getPrice());
            }
        }
        nursingPlan_yyh.setPrice(price);

        return nursingPlan_yyhMapper.updateNursingPlan_yyh(nursingPlan_yyh);
    }

    /**
     * 批量删除护理计划
     * 
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlan_yyhByIds(Long[] ids)
    {
        return nursingPlan_yyhMapper.deleteNursingPlan_yyhByIds(ids);
    }

    /**
     * 删除护理计划信息
     * 
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    public int deleteNursingPlan_yyhById(Long id)
    {
        return nursingPlan_yyhMapper.deleteNursingPlan_yyhById(id);
    }
}
